package com.example.fixsys.controller;

import com.example.fixsys.common.Result;
import com.example.fixsys.entity.WebObject;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController("object")
public class WebObjectController {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @GetMapping("/getObject")
    public Result<?> GetObject(){
        String sql = "select * from webobject";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return Result.success(list);
    }
    @PutMapping("/addObject")
    public Result<?> AddObject(@RequestBody WebObject webObject){
        String sql="insert into webobject(objectNo,objectContent) VALUES (?,?)";
        try{
            jdbcTemplate.update(sql, new Object[]{webObject.getObjectNo(),webObject.getObjectContent()});
        }catch(Exception e){
            return Result.error("12138","项目已存在");
        }
        return Result.success();
    }

    @PostMapping("/updateObject")
    public Result<?> UpdateObject(@RequestBody WebObject webObject){
        String sql="update webobject set objectContent=? where objectNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{webObject.getObjectContent(),webObject.getObjectNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteObject")
    public Result<?> DeleteObject(@RequestBody WebObject webObject){
        String sql="delete from webobject where objectNo=?";
        try{
            jdbcTemplate.update(sql, new Object[]{webObject.getObjectNo()});
        }catch (Exception e){
            return Result.error("12138","项目不存在");
        }
        return Result.success();
    }
}
